package ch.gibb.learnit.client.controllers;

import ch.gibb.learnit.client.util.soap.Category;
import ch.gibb.learnit.client.util.soap.Word;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Controller for the questioning
 *
 * @author Bernard Jaquet
 * @version 1.0
 */
public class QuestionAction extends MasterAction {
    //IN
    private String userResponse;
    private String validation;
    private String form;
    private int questionId;
    private String categoryId;

    //OUT
    private String question;
    private int progress;
    private String systemResponse;
    private List<Category> userCategorys;

    //Both
    private String wordId;

    //Class
    private final Logger masterLogger = Logger.getLogger(QuestionAction.class);

    @Override
    public int openPage() {
        // Setting page title
        super.title = "Abfragen";

        // Check if user is logged in
        if(!loginCheck())
            return 403;

        // Getting categories
        try {
            userCategorys = client.getCategories(getUser());
            if(userCategorys == null)
                getErrors().addMessage("Wegen eines internen Fehlers konnten die Kategorien nicht geladen werden");
        } catch (Exception e) {
            getErrors().addMessage("Die Kategorien konnten wegen eines internen Fehlers nicht geladen werden");
            masterLogger.error("Could not get categories because " + e.getMessage(), e);
        }


        // Logic for the request of the validation
        if (form != null && form.equals("Validieren")) {
            Word w = client.getWord(getUser(), Integer.valueOf(wordId));
            if(w == null){
                getErrors().addMessage("Wegen eines interen Fehlers konnte die Lernkarte nicht geladen werden");
                return 0;
            }
            systemResponse = w.getA().getValue();
            question = w.getQ().getValue();
            return 1;
        }

        // Logic if responce was correct
        if (validation != null && validation.equals("Richtig")) {
            if(!client.respondWord(getUser(), client.getWord(getUser(), Integer.valueOf(wordId)), true))
                getErrors().addMessage("Wegen eines interen Fehler konnte Ihr Vortschritt nicht eingetragen werden");
        }

        // Logic if word was wrong
        if (validation != null && validation.equals("Falsch")) {
            if(!client.respondWord(getUser(), client.getWord(getUser(), Integer.valueOf(wordId)), false))
                getErrors().addMessage("Wegen eines interen Fehler konnte Ihr Vortschritt nicht eingetragen werden");
        }

        // Setting up for a new word
        try {
            Word w;
            if (categoryId != null && !categoryId.equals("") && !categoryId.equals("0"))
            w = client.getWord(getUser(), client.getCategory(getUser(), Integer.valueOf(categoryId)));
            else
                w = client.getWord(getUser());
            if(w == null)
                return 2;
            question = w.getQ().getValue();
            wordId = String.valueOf(w.getId());
        } catch (Exception e) {
            getErrors().addMessage("Lernkarte konnte wegen eines internen Fehlers nicht empfangen werden");
            masterLogger.error("Could not get Word because " + e.getMessage(), e);
        }
        return 0;
    }

    public String getUserResponse() {
        return userResponse;
    }

    public void setUserResponse(String userResponse) {
        this.userResponse = userResponse;
    }

    public String getValidation() {
        return validation;
    }

    public void setValidation(String validation) {
        this.validation = validation;
    }

    public String getForm() {
        return form;
    }

    public void setForm(String form) {
        this.form = form;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public String getSystemResponse() {
        return systemResponse;
    }

    public void setSystemResponse(String systemResponse) {
        this.systemResponse = systemResponse;
    }

    public int getQuestionId() {
        return questionId;
    }

    public void setQuestionId(int questionId) {
        this.questionId = questionId;
    }

    public String getWordId() {
        return wordId;
    }

    public void setWordId(String wordId) {
        this.wordId = wordId;
    }

    public List<Category> getUserCategorys() {
        return userCategorys;
    }

    public void setUserCategorys(List<Category> userCategorys) {
        this.userCategorys = userCategorys;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }
}
